﻿using System.Data;

namespace Luka;

/// <summary>
/// Datatable 类型扩展
/// </summary>
public static class DataTableExtension
{
    /// <summary>
    /// 转换为DataTable
    /// </summary>
    public static DataTable ToDataTable<T>(this IEnumerable<T> data)
    {
        var dataTable = new DataTable();
        // 获取模型类型的属性列表
        var properties = typeof(T).GetProperties();
        // 创建 DataTable 的列
        foreach (var prop in properties)
        {
            dataTable.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
        }
        // 将数据添加到 DataTable
        foreach (var item in data)
        {
            var values = new object[properties.Length];
            for (var i = 0; i < properties.Length; i++)
            {
                values[i] = properties[i].GetValue(item, null) ?? "";
            }
            dataTable.Rows.Add(values);
        }
        return dataTable;
    }

    /// <summary>
    /// DataTable To List
    /// </summary>
    public static List<T> ToList<T>(this DataTable dataTable) where T : new()
    {
        var list = new List<T>();

        foreach (DataRow row in dataTable.Rows)
        {
            var item = new T();

            foreach (DataColumn column in dataTable.Columns)
            {
                var property = typeof(T).GetProperty(column.ColumnName);
                if (property != null && row[column] != DBNull.Value)
                {
                    property.SetValue(item, row[column]);
                }
            }
            list.Add(item);
        }
        return list;
    }
}